<?php
/**
 * Created by PhpStorm.
 * User: bzyy
 * Date: 2015/7/4
 * Time: 11:44
 *
 * 添加到队里里面的请求处理内容
 *
 * 思路步骤：
 * 缓存中读取商品的信息
 * 检测是否已经在队列里面了
 * ajax返回结果
 */

session_start();

require_once './class/defind.php';
require_once './class/redis.php';
require_once './class/bzyy.php';

$session_id = session_id();
$goods_id = isset($_REQUEST['goods_id'])?intval($_REQUEST['goods_id']):0;
if(empty($goods_id))
{
    ajax_return(FLAG_ERROR,'请选择商品id');
}

init_redis();
$redis = $_ENV['redis'];


//检测用户是否已经在队列里面了
$queue_key = make_queue_key($goods_id,$session_id);
$last_time = $redis->get($queue_key);
if($last_time)
{
    ajax_return(FLAG_OK,'您已经在队列中了',array('key'=>$queue_key));

}

//取得商品的数量信息
$goods_num = get_goods_num($goods_id);
if($goods_num == 0)
{
    ajax_return(FLAG_ERROR,'商品已被抢光');
}

$goods_info = get_goods_info($goods_id);
if(empty($goods_info))
{
    ajax_return(FLAG_ERROR,'商品信息不存在');
}

//检测商品时间等等
$now = time();
var_dump($goods_info);exit;

//if($goods_info['start_time'] > $now)
//{
//    ajax_return(FLAG_ERROR,'此商品还未到抢购时间');
//}
//
//if($goods_info['end_time'] < $now)
//{
//    ajax_return(FLAG_ERROR,'此商品抢购已经结束');
//}


$max_queue_num = max_queue_num($goods_num);

$now_queue_num = now_queue_num($goods_id);


if($now_queue_num>$max_queue_num)
{
    ajax_return(FLAG_ERROR,'服务器繁忙，请稍后再来访问');
}

//加入队列
insert_queue($goods_id,$queue_key);

ajax_return(FLAG_OK,'您已进入抢购队列，请耐心等待',array('key'=>$queue_key));




